package com.power.using.mybomb.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.power.using.mybomb.R;
import com.power.using.mybomb.base.ImageLoaderFactory;

import java.text.SimpleDateFormat;

import butterknife.BindView;
import cn.bmob.newim.bean.BmobIMConversation;
import cn.bmob.newim.bean.BmobIMMessage;
import cn.bmob.newim.bean.BmobIMSendStatus;
import cn.bmob.newim.bean.BmobIMUserInfo;
import cn.bmob.newim.listener.MessageSendListener;
import cn.bmob.v3.exception.BmobException;

/**
 * Created by 聪明一只哈 on 2020/3/24.
 * Time: 2020/3/24  10:29
 */
public class SendTextHolder extends BaseViewHolder implements View.OnClickListener, View.OnLongClickListener {

    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.iv_avatar)
    ImageView ivAvatar;
    @BindView(R.id.tv_message)
    TextView tvMessage;
    @BindView(R.id.iv_fail_resend)
    ImageView ivFailResend;
    @BindView(R.id.tv_send_status)
    TextView tvSendStatus;
    @BindView(R.id.progress_load)
    ProgressBar progressLoad;
    private BmobIMConversation c;

    public SendTextHolder(Context context, ViewGroup root, BmobIMConversation c, OnRecyclerViewListener listener) {
        super(context, root, R.layout.item_chat_sent_message, listener);
        this.c = c;
    }

    @Override
    public void bindData(Object o) {

        BmobIMMessage message= (BmobIMMessage) o;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        BmobIMUserInfo info = message.getBmobIMUserInfo();
        ImageLoaderFactory.getLoader().loadAvator(ivAvatar,info!=null?info.getAvatar():null,R.mipmap.head);
        String time = dateFormat.format(message.getCreateTime());
        String content = message.getContent();
        tvMessage.setText(content);
        tvTime.setText(time);

        int status = message.getSendStatus();
        if(status== BmobIMSendStatus.SEND_FAILED.getStatus()){
            ivFailResend.setVisibility(View.VISIBLE);
            progressLoad.setVisibility(View.GONE);
        }else if(status==BmobIMSendStatus.SENDING.getStatus()){
            ivFailResend.setVisibility(View.GONE);
            progressLoad.setVisibility(View.VISIBLE);
        }else{
            ivFailResend.setVisibility(View.GONE);
            progressLoad.setVisibility(View.GONE);
        }


        tvMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toast("点击"+message.getContent());
                if(onRecyclerViewListener!=null){
                    onRecyclerViewListener.onItemLongClick(getAdapterPosition());
                }
            }
        });

        tvMessage.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {

                if(onRecyclerViewListener!=null){
                    onRecyclerViewListener.onItemLongClick(getAdapterPosition());
                }

                return true;
            }
        });

        ivAvatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toast("点击"+info.getName());
            }
        });

        ivFailResend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                c.resendMessage(message, new MessageSendListener() {

                    @Override
                    public void onStart(BmobIMMessage bmobIMMessage) {
                        progressLoad.setVisibility(View.VISIBLE);
                        ivFailResend.setVisibility(View.GONE);
                        tvSendStatus.setVisibility(View.INVISIBLE);
                    }

                    @Override
                    public void done(BmobIMMessage bmobIMMessage, BmobException e) {
                        if(e==null){
                            tvSendStatus.setVisibility(View.VISIBLE);
                            tvSendStatus.setText("已发送");
                            ivFailResend.setVisibility(View.GONE);
                            progressLoad.setVisibility(View.GONE);
                        }else{
                            ivFailResend.setVisibility(View.VISIBLE);
                            progressLoad.setVisibility(View.GONE);
                            tvSendStatus.setVisibility(View.INVISIBLE);
                        }
                    }
                });
            }
        });
    }

    public void showTime(boolean isShow){
        tvTime.setVisibility(isShow?View.VISIBLE:View.GONE);
    }
}
